package com.ruoyi.project.shop.controller;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.PageUtils;
import com.ruoyi.common.utils.bootdo.Query;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.project.shop.domain.ShopUserRoleDO;
import com.ruoyi.project.shop.service.ShopUserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商户和角色关联表
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2021-01-23 18:33:06
 */
 
@Controller
@RequestMapping("/shop/shopUserRole")
public class ShopUserRoleController {
	@Autowired
	private ShopUserRoleService shopUserRoleService;
	
	@GetMapping()
	@RequiresPermissions("shop:shopUserRole:shopUserRole")
	String ShopUserRole(){
	    return "shop/shopUserRole/shopUserRole";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:shopUserRole:shopUserRole")
	public PageUtils list(@RequestParam Map<String, Object> params){

        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&& StringUtils.isNotBlank(params.get("name").toString())){
            List<ShopUserRoleDO> shopUserRoleList = shopUserRoleService.likeList(query);
            int total = shopUserRoleService.likeListCount(query);
            PageUtils pageUtils = new PageUtils(shopUserRoleList, total);
            return pageUtils;
		}

		List<ShopUserRoleDO> shopUserRoleList = shopUserRoleService.list(query);
		int total = shopUserRoleService.count(query);
		PageUtils pageUtils = new PageUtils(shopUserRoleList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:shopUserRole:add")
	String add(){
	    return "shop/shopUserRole/add";
	}

	@GetMapping("/edit/{suserId}")
	@RequiresPermissions("shop:shopUserRole:edit")
	String edit(@PathVariable("suserId") Long suserId,Model model){
		ShopUserRoleDO shopUserRole = shopUserRoleService.get(suserId);
		model.addAttribute("shopUserRole", shopUserRole);
	    return "shop/shopUserRole/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:shopUserRole:add")
	public R save(ShopUserRoleDO shopUserRole){

		if(shopUserRoleService.save(shopUserRole)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:shopUserRole:edit")
	public R update( ShopUserRoleDO shopUserRole){

		shopUserRoleService.update(shopUserRole);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:shopUserRole:remove")
	public R remove( Long suserId){
		shopUserRoleService.remove(suserId);

		return R.ok();

	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:shopUserRole:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] suserIds){
		shopUserRoleService.batchRemove(suserIds);
	
		return R.ok();
	}
	
}
